package com.controller;

import cn.hutool.core.util.StrUtil;
import com.entity.Blog;
import com.entity.Blogger;
import com.service.BlogService;
import com.service.BloggerService;
import com.system.Constants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 博主Controller层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/blogger")
public class BloggerController {

	@Autowired
	private BloggerService bloggerService;
@Autowired
	private BlogService blogService;

	/**
	 * 用户登录
	 * @param blogger
	 * @return
	 */
	@RequestMapping("/login.do")
	@ResponseBody
	public Map login(Blogger blogger, HttpSession session){
//		System.out.println("前端传来的json数据为"+blogger);
		session.setAttribute("username",blogger);
		Map<String, Object> result = new HashMap<>();
		System.out.println(blogger);
		Subject subject= SecurityUtils.getSubject();
//		String password=new String((char[]) IToken.blogger.getPassword());
		UsernamePasswordToken token=new UsernamePasswordToken(blogger.getUserName(),blogger.getPassword());
		try{
			subject.login(token); // 登录验证
			result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
			result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);
			result.put(Constants.RETURN_USERNAME, blogger.getUserName());
			result.put(Constants.RETURN_ID, blogger.getId());
			return result;
		}catch(Exception e){
			e.printStackTrace();
			result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
			result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
			return result;
		}
	}
/**
 * 用户注册
 */
@RequestMapping("register.do")
@ResponseBody
public Map<String, Object> addGoods(Blogger blogger) {
	System.out.println("注册前端传入："+blogger);

	Map<String, Object> result = new HashMap<>();

	//重名校验
	String username=blogger.getUserName();
	if(bloggerService.getByUserName(username)!=null){
		result.put(Constants.RETURN_CODE, Constants.CODE_EXISTERROR);
		result.put(Constants.RETURN_MSG, Constants.MSG_Exist);

		return result;
	}
	// 校验参数
	if(StrUtil.isEmpty(blogger.getUserName()) || StrUtil.isEmpty(blogger.getPassword())
			|| StrUtil.isEmpty(blogger.getProfile())) {

		result.put(Constants.RETURN_CODE, Constants.CODE_PERROR);
		result.put(Constants.RETURN_MSG, Constants.PARAM_ERROR);

		return result;
	}

	try {
		// 业务层写在try catch块中
		bloggerService.addBlogger(blogger);
		result.put(Constants.RETURN_CODE, Constants.CODE_SUCCESS);
		result.put(Constants.RETURN_MSG, Constants.MSG_SUCCESS);

	}catch (Exception e) {
		e.printStackTrace();
		result.put(Constants.RETURN_CODE, Constants.CODE_ERROR);
		result.put(Constants.RETURN_MSG, Constants.MSG_ERROR);
	}

	return result;
}
	/**
	 * 关于博主
	 * @author Administrator
	 *
	 */
	@RequestMapping("/bloggerIndex/{id}")
	public ModelAndView bloggerIndex(@PathVariable("id") Integer id) throws Exception{
		ModelAndView mav=new ModelAndView();
		Blogger blogger=bloggerService.getByUserId(id);
		mav.addObject("blogger",blogger);
		mav.addObject("pageTitle", "关于博主—倾城博客系统");
		mav.addObject("mainPage", "/foreground/blogger/info.jsp");
		mav.setViewName("pages/blogg/mainTemp");
		return mav;
	}
	@RequestMapping("/tour.do")
	public ModelAndView tour(HttpSession session) throws Exception{
		ModelAndView mav=new ModelAndView();
		session.setAttribute("username",new Blogger());
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", null);
		map.put("size", null);
		map.put("typeId",null);
		map.put("releaseDateStr",null);
		map.put("bloggerId", 100);
		List<Blog> blogList=blogService.list(map);
		//博客首页缩略图
		for(Blog blog:blogList){
			//创建图片集合
			List<String> imageList=blog.getImageList();
			//博客信息content
			String blogInfo=blog.getContent();
			//Jsoup截图图片jpg提取
			Document doc= Jsoup.parse(blogInfo);
			Elements jpgs=doc.select("img[src$=.jpg]");
			Elements pngs=doc.select("img[src$=.png]");
			for(int i=0;i<jpgs.size();i++){
				Element jpg=jpgs.get(i);
				imageList.add(jpg.toString());
				if(i==2){
					break;
				}
			}
			for(int i=0;i<pngs.size();i++){
				Element png=pngs.get(i);
				imageList.add(png.toString());
				if(i==2){
					break;
				}
			}
		}
		mav.addObject("blogList", blogList);
		mav.addObject("pageTitle", "倾城博客系统");
		mav.addObject("bloggerId",100);
		mav.addObject("mainPage", "/foreground/blog/list.jsp");
		mav.setViewName("/pages/blogg/mainTemp");
		return mav;
	}

}
